/**
 * 时间格式化函数
 * @param {Date|number} date - 时间对象或时间戳 
 * @param {string} [format='yyyy-mm-dd hh:MM:ss'] - 格式字符串
 * @returns {string} 格式化后的时间字符串 
 */
export function formatTime(date, format = 'yyyy-mm-dd hh:MM:ss') {
    const d = typeof date === 'number' ? new Date(date) : date;

    // 补零函数
    const pad = (n, len = 2) => String(n).padStart(len, '0');

    // 时间组件
    const year = d.getFullYear();
    const month = d.getMonth() + 1;
    const day = d.getDate();
    const hours = d.getHours();
    const minutes = d.getMinutes();
    const seconds = d.getSeconds();
    const milliseconds = d.getMilliseconds();

    // 替换规则
    const replacements = {
        'yyyy': pad(year, 4),
        'yy': pad(year % 100),
        'mm': pad(month),
        'm': String(month),
        'dd': pad(day),
        'd': String(day),
        'hh': pad(hours),
        'h': String(hours),
        'MM': pad(minutes),
        'M': String(minutes),
        'ss': pad(seconds),
        's': String(seconds),
        'sss': pad(milliseconds, 3),
        's.s': `${seconds}.${Math.floor(milliseconds / 100)}`,
        'ss.ss': `${pad(seconds)}.${pad(Math.floor(milliseconds / 10), 2)}`
    };

    // 处理自定义毫秒格式 (如 s.sss)
    const msMatch = format.match(/(s+)(\.)(s+)/);
    if (msMatch) {
        const [full, s1, dot, s2] = msMatch;
        const msPrecision = s2.length;
        const msValue = (milliseconds / 1000).toFixed(msPrecision).slice(2);
        replacements[full] = `${pad(seconds)}.${msValue}`;
    }

    // 执行替换
    return format.replace(
        /yyyy|yy|mm|m|dd|d|hh|h|MM|M|ss\.ss|s\.s|sss|ss|s/g,
        match => replacements[match] || match
    );
}


export function getToday() {
 const now = new Date();
 const start = new Date(now);
 start.setHours(0, 0, 0, 0);
 
 const end = new Date(now);
 end.setHours(23, 59, 59, 999);
 
 return {
   start: start.getTime(),
   end: end.getTime(),
 };
}

export function getThisWeek() {
    const now = new Date();
    const dayOfWeek = now.getDay() || 7; // 将周日(0)转换为7
    
    const start = new Date(now);
    start.setDate(now.getDate() - (dayOfWeek - 1));
    start.setHours(0, 0, 0, 0);
    
    const end = new Date(start);
    end.setDate(start.getDate() + 6);
    end.setHours(23, 59, 59, 999);
    
    return {
      start: start.getTime(),
      end: end.getTime(),
    };
  }

  export function getThisYear() {
    const now = new Date();
    
    const start = new Date(now.getFullYear(), 0, 1);
    start.setHours(0, 0, 0, 0);
    
    const end = new Date(now.getFullYear(), 11, 31);
    end.setHours(23, 59, 59, 999);
    
    return {
      start: start.getTime(),
      end: end.getTime(),
    };
  }

  export function getThisMonth() {
    const now = new Date();
    
    // 本月第一天
    const start = new Date(now.getFullYear(), now.getMonth(), 1);
    start.setHours(0, 0, 0, 0);
    
    // 本月最后一天（下个月第0天就是上个月最后一天）
    const end = new Date(now.getFullYear(), now.getMonth() + 1, 0);
    end.setHours(23, 59, 59, 999);
    
    return {
      start: start.getTime(),
      end: end.getTime()
    };
  }